package com.log4jviewer.filters;

import java.util.List;

import junit.framework.Assert;

import org.junit.Test;

/**
 * Test class for checking the loading of XML config file
 * 
 * @author <a href="mailto:rd.ryly@gmail.com">Ruslan Diachenko</a>
 */
public class ConfigFilterLoaderTest {

    private static final String LOADER_FILE_PATH = "testinputs/log4jviewer/filters/ConfigLoaderTestInput.xml";

    @Test
    public void testLoader() throws Exception {
        ConfigFilterLoader configLoader = new ConfigFilterLoader();
        List<FilterModel> filterModels = configLoader.loadFilters(LOADER_FILE_PATH);

        for (int i = 0; i < filterModels.size(); i++) {
            for (int j = 0; j < filterModels.get(i).getItemsCount(); j++) {
                if (i == 0) {

                    Assert.assertEquals("Filter1", filterModels.get(i).getFilterName());
                    Assert.assertEquals("Show all logs with level DEBUG, or hide all messages with 'select * from'",
                            filterModels.get(i).getFilterDescr());

                    if (j == 0) {
                        Assert.assertEquals("and", filterModels.get(i).getItem(j).getLogicOperand().getValue());
                        Assert.assertEquals("level", filterModels.get(i).getItem(j).getFieldType().getValue());
                        Assert.assertEquals(true, filterModels.get(i).getItem(j).isInclude());
                        Assert.assertEquals("debug", filterModels.get(i).getItem(j).getPattern());
                        Assert.assertEquals(true, filterModels.get(i).getItem(j).isEnabled());
                    }
                    if (j == 1) {
                        Assert.assertEquals("or", filterModels.get(i).getItem(j).getLogicOperand().getValue());
                        Assert.assertEquals("message", filterModels.get(i).getItem(j).getFieldType().getValue());
                        Assert.assertEquals(false, filterModels.get(i).getItem(j).isInclude());
                        Assert.assertEquals("select * from", filterModels.get(i).getItem(j)
                                .getPattern());
                        Assert.assertEquals(true, filterModels.get(i).getItem(j).isEnabled());
                    }
                }
                if (i == 1) {

                    Assert.assertEquals("Filter2", filterModels.get(i).getFilterName());
                    Assert.assertEquals("Show line# 7", filterModels.get(i).getFilterDescr());

                    if (j == 0) {
                        Assert.assertEquals("and", filterModels.get(i).getItem(j).getLogicOperand().getValue());
                        Assert.assertEquals("line", filterModels.get(i).getItem(j).getFieldType().getValue());
                        Assert.assertEquals(true, filterModels.get(i).getItem(j).isInclude());
                        Assert.assertEquals("7", filterModels.get(i).getItem(j).getPattern());
                        Assert.assertEquals(false, filterModels.get(i).getItem(j).isEnabled());
                    }
                }
            }
        }
        Assert.assertEquals(2, filterModels.size());
    }
}
